home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / Thread.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  3KB  |  122 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // Thread.cpp: Implementierung der Klasse CThread.
  20. //
  21. //////////////////////////////////////////////////////////////////////
  22.  
  23. #include "stdafx.h"
  24. #include "Thread.h"
  25.  
  26. //////////////////////////////////////////////////////////////////////
  27. // Konstruktion/Destruktion
  28. //////////////////////////////////////////////////////////////////////
  29.  
  30. CThread::CThread()
  31. {
  32.     m_hThread = 0;
  33.     m_dwThreadId = NULL;
  34.     m_hEventStarted = CreateEvent(0, FALSE, FALSE, NULL);
  35.     m_started = false;
  36. }
  37.  
  38. CThread::~CThread()
  39. {
  40.     CloseHandle(m_hEventStarted);
  41.     CloseHandle(m_hThread);
  42. }
  43.  
  44. BOOL CThread::Create(int nPriority /*=THREAD_PRIORITY_NORMAL*/, DWORD dwCreateFlags /*=0*/)
  45. {
  46.     m_hThread=CreateThread(0, 0, ThreadProc, this, dwCreateFlags, &m_dwThreadId);
  47.     if (!m_hThread)
  48.     {
  49.         delete this;
  50.         return FALSE;
  51.     }
  52.     ::SetThreadPriority(m_hThread, nPriority);
  53.     return TRUE;
  54. }
  55.  
  56. BOOL CThread::PostThreadMessage(UINT message, WPARAM wParam, LPARAM lParam)
  57. {
  58.     BOOL res=::PostThreadMessage(m_dwThreadId, message, wParam, lParam);;
  59.     ASSERT(res);
  60.     return res;
  61. }
  62.  
  63. DWORD CThread::ResumeThread()
  64. {
  65.     DWORD res=::ResumeThread(m_hThread);
  66.     if (!m_started)
  67.     {
  68.         WaitForSingleObject(m_hEventStarted, INFINITE);
  69.     }
  70.     return res;
  71. }
  72.  
  73. DWORD CThread::SuspendThread()
  74. {
  75.     return ::SuspendThread(m_hThread);
  76. }
  77.  
  78. DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
  79. {
  80.     return ((CThread *)lpParameter)->Run();
  81. }
  82.  
  83. DWORD CThread::Run()
  84. {
  85.     InitInstance();
  86.     SetEvent(m_hEventStarted);
  87.     m_started = true;
  88.     MSG msg;
  89.     while (GetMessage(&msg, 0, 0, 0))
  90.     {
  91.         TranslateMessage(&msg);
  92.         if (!msg.hwnd)
  93.             OnThreadMessage(msg.message, msg.wParam, msg.lParam);
  94.         DispatchMessage(&msg);
  95.     }
  96.     DWORD res=ExitInstance();
  97.     delete this;
  98.     return res;
  99. }
  100.  
  101. int CThread::OnThreadMessage(UINT Msg, WPARAM wParam, LPARAM lParam)
  102. {
  103.     return 0;
  104. }
  105.  
  106. BOOL CThread::InitInstance()
  107. {
  108.     return TRUE;
  109. }
  110.  
  111. DWORD CThread::ExitInstance()
  112. {
  113.     return 0;
  114. }
  115.  
  116. BOOL CThread::SetPriority(int nPriority)
  117. {
  118.     if (!m_hThread)
  119.         return false;
  120.  
  121.     return ::SetThreadPriority(m_hThread, nPriority);
  122. }